package com.hsj.goldstore.utils.rxjava;

import android.util.Log;

import com.hsj.goldstore.utils.LogUtil;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/3/22.
 */

public class ObserCallback<T> implements Observer<T> {
    private ApiCaback<T> apiCaback;

    public ObserCallback(ApiCaback<T> apiCaback) {
        this.apiCaback = apiCaback;
    }

    @Override
    public void onSubscribe(Disposable d) {
    }

    @Override
    public void onNext(T value) {
        apiCaback.onSuccess(value);
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof HttpException) {
            HttpException exception = (HttpException) e;
            apiCaback.onFailure(exception.code(), exception.getMessage());
        } else {
            apiCaback.onFailure(-1, e.getMessage());
        }
    }

    @Override
    public void onComplete() {
    }
}
